package cn.scw.project.facility.service.impl;

import cn.scw.common.exception.CustomException;
import cn.scw.common.utils.StringUtils;
import cn.scw.common.utils.bean.ToolUtils;
import cn.scw.project.facility.domain.FacPrintPrice;
import cn.scw.project.facility.domain.netework.bean.FacPrintPriceAddReq;
import cn.scw.project.facility.domain.vo.FacPrintPriceResVo;
import cn.scw.project.facility.domain.vo.FacPrintPriceVo;
import cn.scw.project.facility.mapper.FacPrintPriceMapper;
import cn.scw.project.facility.service.IFacPrintPriceService;
import cn.scw.project.facility.service.IFacPrintTypeService;
import cn.scw.project.system.service.ISysDictDataService;
import com.alibaba.fastjson.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * 默认打印价格Service业务层处理
 *
 * @author api
 * @date 2020-05-20
 */
@Service
public class FacPrintPriceServiceImpl implements IFacPrintPriceService {
    @Autowired
    private FacPrintPriceMapper facPrintPriceMapper;
    @Autowired
    private ISysDictDataService iSysDictDataService;
    @Autowired
    private IFacPrintTypeService iFacPrintTypeService;

    /**
     * 查询默认打印价格
     *
     * @param id 默认打印价格ID
     * @return 默认打印价格
     */
    @Override
    public FacPrintPrice selectFacPrintPriceById(Long id) {
        return facPrintPriceMapper.selectFacPrintPriceById(id);
    }

    @Override
    public FacPrintPriceVo selectFacPrintPriceVoById(Long id) {
        return facPrintPriceMapper.selectFacPrintPriceVoById(id);
    }

    /**
     * 查询默认打印价格列表
     *
     * @return 默认打印价格
     */
    @Override
    public List<FacPrintPriceResVo> selectFacPrintPriceList() {
        List<FacPrintPriceVo> list= facPrintPriceMapper.selectFacPrintPriceList();
        return this.listToResVo(list);
    }
    @Override
    public List<FacPrintPriceResVo> listToResVo(List<FacPrintPriceVo> list){
        JSONObject json = new JSONObject();
        for (FacPrintPriceVo vo : list) {
            String key = vo.getType() + vo.getPaperNumber();
            FacPrintPriceResVo resVo = json.getObject(key, FacPrintPriceResVo.class);
            if (resVo == null) {
                resVo = new FacPrintPriceResVo();
                resVo.setFacilityCode(vo.getFacilityCode());
                resVo.setPaperNumber(vo.getPaperNumber());
                resVo.setType(vo.getType());
                resVo.setTypeValue(vo.getTypeValue());
            }
            if (vo.getIsColours()) {
                resVo.setColorPrice(vo.getPrice());
                resVo.setColorId(vo.getId());
            } else {
                resVo.setNoColorId(vo.getId());
                resVo.setNoColorPrice(vo.getPrice());
            }
            json.put(key,resVo);
        }
        List<FacPrintPriceResVo> resVoList=new ArrayList<>();
        for (Object value : json.values()) {
            FacPrintPriceResVo resVo = (FacPrintPriceResVo) value;
            resVoList.add(resVo);
        }
        return resVoList;
    }

    /**
     * 新增默认打印价格
     *
     * @param req 默认打印价格
     * @return 结果
     */
    @Override
    public int insertFacPrintPrice(FacPrintPriceAddReq req) {
        if (StringUtils.isBlank(req.getType()))
            throw new CustomException("请选择打印类型");
        if (!iFacPrintTypeService.existsByCode(req.getType()))
            throw new CustomException("请选择正确的打印类型");
        if (req.getIsColours() == null)
            throw new CustomException("请选择是否彩色");
        if (req.getIsDoubleFaced() == null)
            req.setIsDoubleFaced(false);
//            throw new CustomException("请选择是否双面");
        if (req.getPaperNumber() == null || req.getPaperNumber() < 1)
            throw new CustomException("请输入正确的纸张数");
        if (this.existsByTypeAndIsColoursAndIsDoubleFacedAndPaperNumber(req.getType(), req.getIsColours(), req.getIsDoubleFaced(), req.getPaperNumber()))
            throw new CustomException("已存在此设置的价格");
        if (req.getPrice() == null || req.getPrice() <= 0)
            throw new CustomException("请设置正确的价格");
        FacPrintPrice facPrintPrice = new FacPrintPrice();
        ToolUtils.copyPropertiesIgnoreNull(req, facPrintPrice);
        return facPrintPriceMapper.insertFacPrintPrice(facPrintPrice);
    }

    /**
     * 修改默认打印价格
     *
     * @param req 默认打印价格
     * @return 结果
     */
    @Override
    public int updateFacPrintPrice(FacPrintPriceAddReq req) {
        FacPrintPrice facPrintPrice = this.selectFacPrintPriceById(req.getId());
        if (facPrintPrice == null)
            throw new CustomException("打印价格不存在");
        ToolUtils.copyPropertiesIgnoreNull(req, facPrintPrice);
        if (!iFacPrintTypeService.existsByCode(facPrintPrice.getType()))
            throw new CustomException("请选择正确的打印类型");
        if (facPrintPrice.getPaperNumber() < 1)
            throw new CustomException("请输入正确的纸张数");
        if (facPrintPrice.getPrice() <= 0)
            throw new CustomException("请设置正确的价格");
        if (this.existsByTypeAndIsColoursAndIsDoubleFacedAndPaperNumber(facPrintPrice.getType(), facPrintPrice.getIsColours(), facPrintPrice.getIsDoubleFaced(), facPrintPrice.getPaperNumber()))
            throw new CustomException("已存在此设置的价格");
        return facPrintPriceMapper.updateFacPrintPrice(facPrintPrice);
    }

    /**
     * 批量删除默认打印价格
     *
     * @param ids 需要删除的默认打印价格ID
     * @return 结果
     */
    @Override
    public int deleteFacPrintPriceByIds(Long[] ids) {
        return facPrintPriceMapper.deleteFacPrintPriceByIds(ids);
    }

    /**
     * 删除默认打印价格信息
     *
     * @param id 默认打印价格ID
     * @return 结果
     */
    @Override
    public int deleteFacPrintPriceById(Long id) {
        return facPrintPriceMapper.deleteFacPrintPriceById(id);
    }

    @Override
    public boolean existsByTypeAndIsColoursAndIsDoubleFacedAndPaperNumber(String type, boolean isColours, boolean isDoubleFaced, int paperNumber) {
        return facPrintPriceMapper.existsByTypeAndIsColoursAndIsDoubleFacedAndPaperNumber(type, isColours, isDoubleFaced, paperNumber);
    }

    @Override
    public FacPrintPriceVo selectByTypeAndIsColoursAndIsDoubleFacedAndPaperNumber(String type, boolean isColours, boolean isDoubleFaced, int paperNumber) {
        return facPrintPriceMapper.selectByTypeAndIsColoursAndIsDoubleFacedAndPaperNumber(type, isColours, isDoubleFaced, paperNumber);
    }
}
